# -*- coding: utf8 -*-
import datetime
from kivy import Logger
from kivy.properties import StringProperty, BooleanProperty, NumericProperty, ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from EasyLangLearnWidgets import MessageBox, ListBox


class EasyLangLearnInputGroup(GridLayout):
    pass


class EasyLangLearnInput(BoxLayout):
    label = StringProperty()
    id = NumericProperty(0)
    label_size = NumericProperty(1)

    def __init__(self, **kwargs):
        super(EasyLangLearnInput, self).__init__(**kwargs)

        if 'label' in kwargs:
            self.label = kwargs['label']

        if 'id' in kwargs:
            self.id = kwargs['id']

        if 'label_size' in kwargs:
            self.label_size = kwargs['label_size']


class EasyLangLearnInputSwitch(EasyLangLearnInput):
    state = BooleanProperty(True)
    switch_input = ObjectProperty()

    def __init__(self, **kwargs):
        super(EasyLangLearnInputSwitch, self).__init__(**kwargs)

        if 'state' in kwargs:
            self.state = kwargs['state']


class EasyLangLearnInputText(EasyLangLearnInput):
    text = StringProperty()


class EasyLangLearnInputInt(EasyLangLearnInput):
    number = NumericProperty(0)

    def on_validate(self, item):
        try:
            self.number = int(item.text)
        except ValueError, err:
            MessageBox("Позволени са само цели числа", title="Грешка").open()
            if self.number:
                self.number = 0
            else:
                item.text = '0'

    def on_focus(self, item):
        if not item.focus:
            self.on_validate(item)


class EasyLangLearnInputFloat(EasyLangLearnInputInt):
    def on_validate(self, item):
        try:
            self.number = float(item.text)
        except ValueError, err:
            MessageBox("Позволени са само реални числа", title="Грешка").open()
            if self.number:
                self.number = 0
            else:
                item.text = '0'


class EasyLangLearnInputDate(EasyLangLearnInput):
    text = StringProperty('01.01.1900')
    date = datetime.date(1900, 1, 1)

    def update_date(self):
        self.date = datetime.date(int(self.text[6:]), int(self.text[3:5]), int(self.text[0:2]))

    def on_validate(self, item):
        try:
            self.text = item.text

            self.update_date()
        except ValueError, err:
            MessageBox("Невалидна дата (дд.мм.гггг)", title="Грешка").open()
            if self.text != '01.01.1900':
                self.text = '01.01.1900'
            else:
                item.text = '01.01.1900'

            self.update_date()

    def on_focus(self, item):
        if not item.focus:
            self.on_validate(item)


class EasyLangLearnInputSelect(EasyLangLearnInput):
    button = ObjectProperty()

    def __init__(self, **kwargs):
        super(EasyLangLearnInputSelect, self).__init__(**kwargs)

        self.list = ListBox()
        self.id

    def open_list(self):
        self.list.open(self.button)

    def on_select(self, item):
        Logger.info("SELECTED")
        self.button.text = item.text

